(function($, im) {
	document.querySelector('.takePhoto').addEventListener('tap', function(event) {
		var btnArray = [{
			title: "拍照"
		}, {
			title: "从相册选择"
		}];

		var uploadImg = function(path) {
			var path = plus.io.convertLocalFileSystemURL(path);
			console.log(path);
			var ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
			var name = im.uid + '-' + Date.parse(new Date()) + '.' + ext;
			var sname = im.uid + '-' + Date.parse(new Date()) + '_s.' + ext;
			im.sendMsg({
				type: 'image',
				content: path
			}, 1);
			im.closeFooterBox();
			im.playsendSound('pic');
			im.ajaxuploadImg(path);
			/*
			var task = plus.uploader.createUpload(upurl, { // gz-修改 
					method: "POST",
					blocksize: 2048000,
					priority: 100
			}, 
			 //http://up.yuxw.net/index.php?url=gatc&file=
 
				function(t, status) {
					//var el = doc.querySelector("[data-id='" + webpath + "']");
					//var parents = el.parentNode;
					console.log(status);
					console.log(JSON.stringify(t));
					if(status == 200) {
						//console.log(t.responseText);
						//var rt = JSON.parse(t.responseText);
						var msg = {
							type: 'image',
							content: 'upload/file/' + sname
						};
						var res = {};
						res.url = 'upload/file/' + name;
						res.surl = 'upload/file/' + sname;
						res.size = t.totalSize;
						//res.name = 
						res.src = serverurl + res.surl;
						res.bsrc = serverurl + res.url;
						var files = path.split("/");
						var name = files.pop();
						res.name = name;
						var exts = name.split(".");
						res.ext = exts.pop();
						msg.extras = res;
						var el = document.querySelector("[src='" + path + "']");
						//res.src = serverurl + res.surl;
						//res.bsrc = serverurl + res.url;
						console.log(res.src);
						el.setAttribute('src', res.src);
						el.setAttribute('bsrc', res.bsrc);
						el.setAttribute('fname', res.name);
						el.setAttribute('fsize', res.size);
						el.setAttribute('ftype', res.type);
						var parent = el.parentNode.parentNode.parentNode;
						var id = parent.getAttribute('msg-id');
						var extras = JSON.stringify(res);
						chat.edit(im.roomId, id, 'extras', extras);
						//console.log(JSON.stringify(msg));
						//rt.filename = filename;
						im.sendMsg(msg, 2);
						//el.querySelector('span').innerHTML = rt.size + 'kb';
						mui.toast("发送成功！");
					} else {
						console.log('转ajaxuploadImg');
						im.ajaxuploadImg(path);
						//el.setAttribute('data-path', filepath);
						//console.log(filepath);
						//mui.toast("发送失败！");
					}
				},
				function(err) {
					console.log(JSON.stringify(err));
				});

			task.addFile(path, {
				key: "file",
				name: name
			});
			task.addData('name', name);
			task.start();*/
			/*
			 */
		};
		plus.nativeUI.actionSheet({
			title: "选择照片",
			cancel: "取消",
			buttons: btnArray
		}, function(e) {
			var index = e.index;
			switch(index) {
				case 0:
					break;
				case 1:
					var cmr = plus.camera.getCamera();
					cmr.captureImage(function(path) {
						uploadImg(path);
					}, function(err) {});
					break;
				case 2:
					//多选图片
					plus.gallery.pick(function(path) {
						uploadImg(path);
						/*
						for(var i in e.files) {
							uploadImg(e.files[i]);
						}*/
					}, function(e) {}, {
						filter: "image"
					});
					break;
			}
		});
	}, false);

}(mui, im));